package org.oschina.helper.topic;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.oschina.helper.JsonHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 订阅监听抽象类
 */
public abstract class AbstractTopicListener implements MessageListener {
	
	private final static Logger logger=LoggerFactory.getLogger(AbstractTopicListener.class);
	
	/**
	 * 类型
	 * @return
	 */
	public abstract Class getType();
	
	/**
	 * 监听的key
	 * @return
	 */
	public abstract String getKey();

	/**
	 * 处理
	 */
	public abstract void handle(Object message);

	@Override
	public final void onMessage(Message message)
	{
		TextMessage textMessage=(TextMessage)message;
		try
		{
			Object parseMessage=JsonHelper.parse(textMessage.getText(), getType());
			if(parseMessage==null)
				return;
			
			handle(parseMessage);
		}
		catch (JMSException e)
		{
			logger.error("jms - 接收订阅消息错误[key:{}]",getKey(),e);
		}
	}
}